package com.icez.base_compose_framework.util

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.compositeOver
import androidx.compose.ui.graphics.luminance
import kotlin.math.max
import kotlin.math.min

/**
 * 颜色工具类
 * @author icez
 */
object BColorUtil {
    /**
     * 颜色对比度
     * @param foreground 前景色
     * @param background 背景色
     */
    fun contrastAgainst(foreground:Color,background: Color):Float{
        val fg = if (foreground.alpha < 1f) foreground.compositeOver(background) else foreground
        val fgLuminance = fg.luminance() + 0.05f
        val bgLuminance = background.luminance() + 0.05f
        return max(fgLuminance, bgLuminance) / min(fgLuminance, bgLuminance)
    }
}